﻿using Module.FlowChart.Model;
using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Presentation.Drawing.Animation;
using System.Collections.Generic;
using System.Drawing;

namespace Module.FlowChart.View
{
    internal static class PPTCreater
    {
        // PPTCreater.CreatPPTSe("1.pptx",new List<FCNode>(Page.Nodes.Values));
        static ShapeType GetNodeShpeType(FCNode node)
        {
            switch (node.Shape)
            {
                case Model.Shape.Non:
                    return ShapeType.Rectangle;
                case Model.Shape.Rect:
                    return ShapeType.Rectangle;
                case Model.Shape.Circular:
                    return ShapeType.Ellipse;
                case Model.Shape.Diamond:
                    return ShapeType.Diamond;
                case Model.Shape.Parallelogram:
                    return ShapeType.Parallelogram;
                default:
                    return ShapeType.Rectangle;
            }
        }
        static IAutoShape AppendNode(ISlide sld, FCNode node)
        {
            //添加一个图形      
            IAutoShape textboxShape = sld.Shapes.AppendShape(GetNodeShpeType(node), node.LogicRect);
            textboxShape.ShapeStyle.LineColor.Color = Color.Black;
            textboxShape.Line.SolidFillColor.Color = Color.Black;
            textboxShape.Fill.FillType = FillFormatType.Solid;
            textboxShape.Fill.SolidColor.Color = node.BackGroundColor;
            //（默认有一个空白段落）           
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].Text = node.GetNodeText();

            //设置段落中文字的字体、大小、颜色及段落的对齐方式、段首缩进距离          
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].LatinFont = new TextFont(node.Font.FontFamily.Name);
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].EastAsianFont = new TextFont(node.Font.FontFamily.Name);
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].FontHeight = node.Font.SizeInPoints;
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.FillType = FillFormatType.Solid;
            textboxShape.TextFrame.Paragraphs[0].TextRanges[0].Fill.SolidColor.Color = Color.Black;
            textboxShape.TextFrame.Paragraphs[0].Alignment = TextAlignmentType.Center;


            return textboxShape;
        }
        static List<IAutoShape> AppendLine(ISlide sld, FCLine line)
        {
            IAutoShape sp = null;
            List<IAutoShape> re = new List<IAutoShape>(line.Points.Length - 1);
            for (int i = 0; i < line.Points.Length - 1; i++)
            {
                int x = line.Points[i].X, y = line.Points[i].Y;
                int w = line.Points[i + 1].X - x;
                int h = line.Points[i + 1].Y - y;
                if (w < 0)
                {
                    x += w;
                    w = -w;
                }
                if (h < 0)
                {
                    y += h;
                    h = -h;
                }
                sp = sld.Shapes.AppendShape(ShapeType.Line,
               new RectangleF(x, y, w, h));
                sp.Line.SolidFillColor.Color = Color.Black;
                re.Add(sp);
            }
            sp.Line.LineEndType = LineEndType.TriangleArrowHead;
            string text = line.GetLineText();
            if (text != "")
            {
                re[0].TextFrame.Text = text;
                re[0].TextFrame.TextRange.Fill.SolidColor.Color = Color.Black;
                re[0].TextFrame.TextRange.Fill.FillType = FillFormatType.Solid;
                re[0].TextFrame.TextRange.FontHeight = 30f;
            }
            return re;
        }
        static internal void CreatPPT_Sequential(string path, List<FCNode> nodes, List<FCLine> lines)
        {
            List<IAutoShape> nodeshapes = new List<IAutoShape>();
            List<List<IAutoShape>> lineshapes = new List<List<IAutoShape>>();
            //新建一个PowerPoint文档（默认包含一页空白幻灯片）           
            Presentation ppt = new Presentation();
            //设置幻灯片大小和方向           
            ppt.SlideSize.Type = SlideSizeType.Screen16x9;
            ppt.SlideSize.Orientation = SlideOrienation.Landscape;
            ISlide sld = ppt.Slides[0];

            foreach (var node in nodes)
                nodeshapes.Add(AppendNode(sld, node));
            foreach (var line in lines)
                lineshapes.Add(AppendLine(sld, line));

            for (int i = 0; i < nodes.Count; i++)
            {
                AnimationEffect effect = sld.Timeline.MainSequence.AddEffect(nodeshapes[i], AnimationEffectType.Box);
                effect.Timing.Duration = 0.5f;
                effect.Timing.RepeatCount = 1;
                effect.IterateTimeValue = 0;
                for (int t = 0; t < lines.Count; t++)
                {
                    var line = lines[t];
                    if (line.To == nodes[i].Id)
                    {
                        bool find = false;
                        for (int j = 0; j < i; j++)
                        {
                            if (nodes[j].Id == line.From)
                            {
                                find = true;
                                break;
                            }
                        }
                        if (find)
                        {
                            foreach (var e in lineshapes[t])
                            {
                                var lineeffect = effect.Effects.AddEffect(e, AnimationEffectType.Box);
                                lineeffect.Timing.TriggerType = AnimationTriggerType.WithPrevious;
                                lineeffect.Timing.Duration = 0.5f;
                                lineeffect.Timing.RepeatCount = 1;
                                lineeffect.IterateTimeValue = 0;
                            }

                        }
                    }
                }


            }

            //保存文档           
            ppt.SaveToFile(path, FileFormat.Pptx2013);
        }
        static internal void CreatPPT_Runable(string path, List<FCNode> nodes, List<FCLine> lines, float scale = 1)
        {
            List<IAutoShape> nodeshapes = new List<IAutoShape>();
            List<List<IAutoShape>> lineshapes = new List<List<IAutoShape>>();
            //新建一个PowerPoint文档（默认包含一页空白幻灯片）           
            Presentation ppt = new Presentation();
            //设置幻灯片大小和方向           
            ppt.SlideSize.Type = SlideSizeType.Screen16x9;
            ppt.SlideSize.Orientation = SlideOrienation.Landscape;
            ISlide sld = ppt.Slides[0];

            foreach (var node in nodes)
                nodeshapes.Add(AppendNode(sld, node));
            foreach (var line in lines)
                lineshapes.Add(AppendLine(sld, line));


            //保存文档           
            ppt.SaveToFile(path, FileFormat.Pptx2013);
        }
        //给形状添加动画效果
        //AnimationEffect effect = shape.Slide.Timeline.MainSequence.AddEffect(shape, AnimationEffectType.FadedSwivel);
        //设置持续时间
        //effect.Timing.Duration = 2;
        //设置重复次数
        //effect.Timing.RepeatCount = 2;
        //设置动画文本效果
        //effect.IterateType = Spire.Presentation.Drawing.TimeLine.AnimateType.Letter;
        //设置动画文本字符之间延迟时间
        //effect.IterateTimeValue = 2;

        ////为幻灯片设置背景图片          
        //string ImageFile = "background.jpg";
        //RectangleF rect = new RectangleF(0, 0, ppt.SlideSize.Size.Width, ppt.SlideSize.Size.Height);
        //ppt.Slides[0].SlideBackground.Fill.FillType = FillFormatType.Picture;
        //IEmbedImage image = ppt.Slides[0].Shapes.AppendEmbedImage(ShapeType.Rectangle, ImageFile, rect);
        //ppt.Slides[0].SlideBackground.Fill.PictureFill.Picture.EmbedImage = image as IImageData;     
    }
}